package com.test.last;


import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;


import android.content.DialogInterface;
import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;



import android.util.Log;
import android.view.View;

import android.widget.AdapterView;

import android.widget.ImageView;
import android.widget.ListView;



import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;



public class Tianqi extends AppCompatActivity implements Runnable, AdapterView.OnItemClickListener, View.OnClickListener,AdapterView.OnItemLongClickListener {
    private static final String TAG = "测试";
    Handler handler;
    ListView listView;
    ImageView imag;
    String url1;
    ArrayList<String> urls;
    ImageView image;
    MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tianqi);

        listView = findViewById(R.id.list2);
        imag = findViewById(R.id.main_iv_city);
        image = findViewById(R.id.main_iv_add);


        handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 2) {
                    ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) msg.obj;
                    adapter = new MyAdapter(Tianqi.this, R.layout.weather_list, list);
                    listView.setAdapter(adapter);
                }
                super.handleMessage(msg);
            }
        };

        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);
        imag.setOnClickListener(this);
        image.setOnClickListener(this);


        Thread t = new Thread(this);
        t.start();

    }


    @Override
    public void run() {
        try {
            Thread.sleep(1000);                      //设置子线程的延迟时间
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Message msg = handler.obtainMessage(2);
        Document doc = null;
        try {
            doc = Jsoup.connect(" http://www.weather.com.cn/textFC/xn.shtml").get();   //使用jsoup爬取网页上的数据，得到一个doc
            Elements tables = doc.getElementsByTag("table");                  // getElementsByTag 获得标签信息，返回一个Elements对象
            Element table = tables.get(0);
            Elements tds = table.getElementsByTag("td");
            ArrayList<HashMap<String, String>> listItems = new ArrayList<>();
            urls = new ArrayList<>();
            for (int i = 12; i < tds.size(); i += 8) {
                Element city = tds.get(i);
                Element Weather = tds.get(i + 4);
                Element temp = tds.get(i + 5);
                Element url = tds.get(i);
                Log.e("TAG", "url ======>>" + url);
                url1 = url.select("a").attr("href");
                Log.e("TAG", "url1 ======>>" + url1);
                HashMap<String, String> map = new HashMap<>();
                urls.add(url1);
                map.put("city", city.text());
                map.put("weather", Weather.text());
                map.put("temp", temp.text());
                listItems.add(map);

            }
            msg.obj = listItems;
            handler.sendMessage(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String url_item = urls.get(position);
        final Uri uri = Uri.parse(url_item);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }//跳转超链接

    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
        Object ob = listView.getItemAtPosition(position);
        Log.i(TAG, "onItemLongClick: 长按列表项position=" + position);
        //删除操作
        //构造对话框进行确认操作
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示").setMessage("请确认是否删除当前数据").setPositiveButton("是",new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.i(TAG, "onClick: 对话框事件处理");
                adapter.remove(ob);
                adapter.notifyDataSetChanged();
                dialog.dismiss();
            }
        }).setNegativeButton("否",null);
        builder.create().show();
        //Log.i(TAG, "onItemLongClick: size=" + listItems.size());

        return true;
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_iv_city:
                Intent intent = new Intent(this, Load.class);
                startActivity(intent);
                break;
            case R.id.main_iv_add:
                Intent intent1 = new Intent(this, Tianqi2.class);
                startActivity(intent1);
                break;

        }
    }//跳转加载登录注册界面

}
